package com.flk.oauth.util;

import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collection;

/**
 * @author xxwy
 * on 2018/10/15 0015
 */
public final class SecurityUtils {

    public static String getCurrentUserUsername() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String currentUserName = null;
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
             currentUserName = authentication.getName();
        }
        return currentUserName;
    }

    public static Collection<? extends GrantedAuthority> getCurrentUserAuthority(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Collection<? extends GrantedAuthority> authorities = null;
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            authorities = authentication.getAuthorities();
        }
        return authorities;
    }
}
